package com.example.jsonquynh;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FavoriteManager {
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "favorite.db";
	public static final String TABLE_FAVORITE = "favorite";
	public static final String COLUMN_ID = "id";
	public static final String COLUMN_TITLE = "name";
	public static final String COLUMN_ADDRESS = "addr";
	public static final String COLUMN_POSITION = "pos";
	public static final String COLUMN_DESCRIPTION = "des";
	public static final String COLUMN_THUMBNAIL = "thumb";
	public static final String COLUMN_IMAGES = "img";

	private SQLiteDatabase database;
	private SQLiteOpenHelper openHelper;

	public FavoriteManager(Context context) {
		openHelper = new SQLiteOpenHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION) {

			@Override
			public void onUpgrade(SQLiteDatabase db, int oldVersion,
					int newVersion) {
				Log.w(FavoriteManager.class.getName(),
						"Upgrading database from version " + oldVersion
								+ " to " + newVersion
								+ ", which will destroy all old data");
				db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITE);
				onCreate(db);
			}

			@Override
			public void onCreate(SQLiteDatabase db) {
				String createTable = "create table " + TABLE_FAVORITE + "("
						+ COLUMN_ID + " text primary key, " + COLUMN_TITLE
						+ " text not null, " + COLUMN_ADDRESS + " text, "
						+ COLUMN_POSITION + " text, " + COLUMN_DESCRIPTION
						+ " text, " + COLUMN_THUMBNAIL + " text, "
						+ COLUMN_IMAGES + " text " + ")";
				db.execSQL(createTable);

			}
		};
		open();

	}
	public void open(){
		database = openHelper.getWritableDatabase();
	}
	public boolean isExitID(String ID) {
		String[] allColumns = { COLUMN_ID };
		Cursor cursor = database.query(TABLE_FAVORITE, allColumns, COLUMN_ID
				+ "='" + ID + "'", null, null, null, null);
		return cursor.getCount() > 0 ? true : false;
	}

	public boolean insertPlace(Place place) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ID, place.getID());
		values.put(COLUMN_TITLE, place.getTitle());
		values.put(COLUMN_ADDRESS, place.getAddress());
		values.put(COLUMN_POSITION, place.getPosition());
		values.put(COLUMN_DESCRIPTION, place.getDescription());
		values.put(COLUMN_THUMBNAIL, place.getThumbnai());
		values.put(COLUMN_IMAGES, place.getImages());
		long insertId = database.insert(TABLE_FAVORITE, null, values);
		return (insertId == -1) ? false : true;
	}

	public void deletePlaceID(String ID) {
		database.delete(TABLE_FAVORITE, COLUMN_ID + "='" + ID + "'", null);
	}

	public List<Place> getAllFavoritePlaces() {
		List<Place> places = new ArrayList<Place>();
		String[] allColumns = { COLUMN_ID, COLUMN_TITLE, COLUMN_ADDRESS,
				COLUMN_POSITION, COLUMN_DESCRIPTION, COLUMN_THUMBNAIL,
				COLUMN_IMAGES };
		Cursor cursor = database.query(TABLE_FAVORITE, allColumns, null, null,
				null, null, COLUMN_TITLE +" ASC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Place place = new Place(cursor.getString(0), cursor.getString(1),
					cursor.getString(2), cursor.getString(3),
					cursor.getString(4), cursor.getString(5),
					cursor.getString(6));
			places.add(place);
			cursor.moveToNext();
		}
		cursor.close();
		return places;
	}

	public void close() {
		database.close();
		openHelper.close();
	}

}
